{extend name="property/public/insidePageBase" /}
{block name="title"}楼栋单元列表{/block}
{block name="body"}
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-inline">
                <label class="layui-form-label">物业选择</label>
                <div class="layui-input-inline">
                    <select name="property_id" lay-filter="property_id" lay-search>
                        <option value="">请选择</option>
                        {foreach $propertyData as $k=>$v}
                        <option value="{$v['id']}">{$v['name']}</option>
                        {/foreach}
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">关键字搜索</label>
                <div class="layui-input-inline">
                    <input type="text" name="keywords" placeholder="房间名称/楼层/备注搜索" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
            </div>
        </form>
        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                {if $addAuth}<button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="addLouDong"> 添加楼栋</button>{/if}
                {if $addLouDongAuth}<button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加单元</button>{/if}
            </div>
        </script>
        <table class="layui-hide" id="propertyUnitList" lay-filter="propertyUnitList"></table>
        <script type="text/html" id="tableBar">
            <div class="layui-btn-group">
                {if $editAuth} <a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit">编辑</a>{/if}
                {if $detailAuth}<a class="layui-btn layui-btn-sm" lay-event="details">详情</a>{/if}
                {if $delAuth}<a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>{/if}
            </div>
        </script>
        <script type="text/html" id="is_show_tpl">
            <input type="checkbox" lay-skin="switch" value="{{ d.id }}" disabled lay-filter="isShow" {{# if(d.is_show== 1) { }}checked{{# } }} lay-text="显示|隐藏"/>
        </script>
    </div>
</div>
{/block}
{block name="js"}
{__block__}
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery, form = layui.form, table = layui.table;
        var tableId = 'propertyUnitList';
        table.render({
            elem: '#' + tableId,
            id: tableId,
            url: '{:url("propertyUnitList")}',
            toolbar: '#toolbar',
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'property_name', width: 198, title: '物业信息'},
                {field: 'full_unit_name', width: 298, title: '楼栋单元名称'},
                {field: 'unit_code', width: 110, title: '编号',  sort: true},
                {field: 'is_show', width: 110, title: '是否显示', templet: '#is_show_tpl', sort: true, align: "center"},
                {field: 'add_time', width: 160, title: '创建时间', sort: true}
                {if $editAuth || $delAuth || $detailAuth}
                , {title: '操作', minWidth: 290, toolbar: '#tableBar'}
                {/if}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            height: 'full-80'
        });
        form.on('submit(data-search-btn)', function (data) {
            table.reload(tableId, {page: {curr: 1}, where: data.field}, 'data');
            return false;
        });
        /**
         * toolbar监听事件
         */
        table.on('toolbar(' + tableId + ')', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var url = '{:url("propertyUnitAdd")}';
                var index = parent.layer.open({
                    title: '添加单元', type: 2, shade: 0.2,  maxmin: true,  area: ['500px', '450px'],  content: url  , btnAlign: 'c' , btn: ['确定', '取消'], yes: function (index, layero) {
                        var iframeWindow = parent.window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                            var field = data.field; //获取提交的字段
                            var loadAdd = parent.layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                            $.post(url, field, function (res) {
                                parent.layer.close(loadAdd);
                                if (res.code === 0) {
                                    table.reload(tableId); //数据刷新
                                    parent.layer.close(index); //关闭弹层
                                    parent.layer.msg(res.msg, {icon: 6, offset: '15px'});
                                } else {
                                    parent.layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            } else if (obj.event === 'addLouDong') {  // 监听添加操作
                var url = '{:url("propertyUnitLouDongAdd")}';
                var index = parent.layer.open({
                    title: '添加楼栋', type: 2, shade: 0.2,  maxmin: true,  area: ['500px', '450px'],  content: url  , btnAlign: 'c' , btn: ['确定', '取消'], yes: function (index, layero) {
                        var iframeWindow = parent.window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                            var field = data.field; //获取提交的字段
                            var loadAdd = parent.layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                            $.post(url, field, function (res) {
                                parent.layer.close(loadAdd);
                                if (res.code === 0) {
                                    table.reload(tableId); //数据刷新
                                    parent.layer.close(index); //关闭弹层
                                    parent.layer.msg(res.msg, {icon: 6, offset: '15px'});
                                } else {
                                    parent.layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(' + tableId + ')', function (obj) {
            console.log(obj)
        });

        table.on('tool(' + tableId + ')', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var url = '{:url("propertyUnitEdit")}?id=' + data.id;
                var index = parent.layer.open({
                    title: '编辑楼栋单元', type: 2,   shade: 0.2,  maxmin: true, area: ['500px', '400px'],   content: url  , btnAlign: 'c'  , btn: ['确定', '取消'] , yes: function (index, layero) {
                        var iframeWindow = parent.window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                            var field = data.field; //获取提交的字段
                            var loadAdd = parent.layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                            $.post(url, field, function (res) {
                                parent.layer.close(loadAdd);
                                if (res.code === 0) {
                                    table.reload(tableId); //数据刷新
                                    parent.layer.close(index); //关闭弹层
                                    parent.layer.msg(res.msg, {icon: 6, offset: '15px'});
                                } else {
                                    parent.layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
                return false;
            } else if (obj.event === 'details') {
                var url = '{:url("propertyUnitDetail")}?id=' + data.id;
                var index = parent.layer.open({
                    title: data.unit_name + ' 详情 ', type: 2, shade: 0.2, maxmin: true, shadeClose: true,  move: false,area: ['1000px', '100%'], offset: 'rt', anim: 2, content: url,btn: false
                });
                return false;
            } else if (obj.event === 'delete') {
                var url = '{:url("propertyUnitDel")}';
                layer.confirm('删除后不可恢复！  是否确认? ', {icon: 3}, function () {
                    var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                    $.post(url, {id: data.id}, function (res) {
                        layer.close(loadAdd);
                        if (res.code === 0) {
                            table.reload(tableId); //数据刷新
                            layer.msg(res.msg, {icon: 6, offset: '15px'});
                        } else {
                            layer.msg(res.msg, {icon: 5, offset: '15px'});
                        }
                    });
                });
            }
        });
    })
        ;
</script>
{/block}